//
// Created by wenyaxi on 24-10-29.
//
#include <common.h>

class Solution {
public:
    vector<vector<int> > permute(vector<int> &nums) {
        vector<vector<int> > ans;
        vector<bool> visited(nums.size(), false);
        vector<int> temp;
        dfs(nums, temp, visited, ans);
        return ans;
    }

    void dfs(vector<int> &nums, vector<int> &stk, vector<bool> &visited,
             vector<vector<int> > &result) {
        const int n = nums.size();
        if (stk.size() == n) {
            result.emplace_back(stk);
            return;
        }

        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                visited[i] = true;
                stk.push_back(nums[i]);
                dfs(nums, stk, visited, result);
                visited[i] = false;
                stk.pop_back();
            }
        }
    }
};

int main(int argc, char *argv[]) {
    Solution s;
    vector<int> nums = {1, 2, 3};
    for (const vector<vector<int> > ans = s.permute(nums); const auto& v: ans) {
        cout << printer::arrayToString(v) << endl;
    }
}
